
//================================================================================
//DBG message process
//================================================================================

import java.io.*;
import java.util.*;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;


public class DBG{
	private String version="2013/08/10";

	private String fileName;
	private Date date=new Date();
	private Locale locale=new Locale("en","US");
	private DateFormat mediumFormat=DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,locale);;
	long oldtime=System.currentTimeMillis();
	private int function=0;


	private final int _ShowOnScreen_	=0x01;
	private final int _SaveFile_		=0x02;
	private final int _ShowDeltaTime_	=0x04;
	//================================================================================
	//
	//================================================================================
	public DBG(){
		fileName="a.txt";

	}

	//================================================================================
	//
	//================================================================================
	public DBG(String fileName){
		this.fileName=fileName;
//System.out.println("ini=0");
		try{
			BufferedWriter bufferedwriter=new BufferedWriter(new FileWriter(this.fileName,false));
			bufferedwriter.write(mediumFormat.format(date));
			bufferedwriter.newLine();
			bufferedwriter.write("-------------------------");
			bufferedwriter.newLine();
			bufferedwriter.close();
		}
		catch(ArrayIndexOutOfBoundsException e){
			e.printStackTrace();
		}
		catch(IOException e){
			e.printStackTrace();
		}
	}

	//================================================================================
	//
	//================================================================================
	public DBG(String fileName,int function){
		this.fileName=fileName;

		this.function=function;

		try{
			BufferedWriter bufferedwriter=new BufferedWriter(new FileWriter(this.fileName,false));
			bufferedwriter.write(mediumFormat.format(date));
			bufferedwriter.newLine();
			bufferedwriter.write("-------------------------");
			bufferedwriter.newLine();

			bufferedwriter.close();

		}
		catch(ArrayIndexOutOfBoundsException e){
			e.printStackTrace();
		}
		catch(IOException e){
			e.printStackTrace();
		}
	}
	//================================================================================
	//save DBG message in file
	//================================================================================
	public void save(String message,boolean newline){
		if((function&_ShowOnScreen_)==_ShowOnScreen_){
			if(newline)
				System.out.println(message);
			else
				System.out.print(message);
		}

		if((function&_SaveFile_)==_SaveFile_){
			try{

				BufferedWriter bufferedwriter=new BufferedWriter(new FileWriter(this.fileName,true));

				if((function&_ShowDeltaTime_)==_ShowDeltaTime_){
					bufferedwriter.write("deltaTime="+String.valueOf(System.currentTimeMillis()-oldtime)+" ms");
					oldtime=System.currentTimeMillis();

					if(newline)
						bufferedwriter.newLine();
				}

				bufferedwriter.write(message);
	
				if(newline)
					bufferedwriter.newLine();

				bufferedwriter.close();
	
			}
			catch(ArrayIndexOutOfBoundsException e){
				e.printStackTrace();	
			}
			catch(IOException e){
				e.printStackTrace();
			}
		}

	}

	public void save(String message){
		if((function&_ShowOnScreen_)==_ShowOnScreen_){
			System.out.println(message);
		}

		if((function&_SaveFile_)==_SaveFile_){
			try{

				BufferedWriter bufferedwriter=new BufferedWriter(new FileWriter(this.fileName,true));

				if((function&_ShowDeltaTime_)==_ShowDeltaTime_){
					bufferedwriter.write("deltaTime="+String.valueOf(System.currentTimeMillis()-oldtime)+" ms");
					oldtime=System.currentTimeMillis();
					bufferedwriter.newLine();
				}

				bufferedwriter.write(message);
	
				bufferedwriter.newLine();
				bufferedwriter.close();
	
			}
			catch(ArrayIndexOutOfBoundsException e){
				e.printStackTrace();	
			}
			catch(IOException e){
				e.printStackTrace();
			}
		}

	}
	public void save(){
		if((function&_ShowOnScreen_)==_ShowOnScreen_){
			System.out.println();
		}

		if((function&_SaveFile_)==_SaveFile_){
			try{

				BufferedWriter bufferedwriter=new BufferedWriter(new FileWriter(this.fileName,true));

				if((function&_ShowDeltaTime_)==_ShowDeltaTime_){
					bufferedwriter.write("deltaTime="+String.valueOf(System.currentTimeMillis()-oldtime)+" ms");
					oldtime=System.currentTimeMillis();
					bufferedwriter.newLine();
				}

//				bufferedwriter.write(message);
	
				bufferedwriter.newLine();
				bufferedwriter.close();
	
			}
			catch(ArrayIndexOutOfBoundsException e){
				e.printStackTrace();	
			}
			catch(IOException e){
				e.printStackTrace();
			}
		}

	}
	//================================================================================
	//================================================================================
	public String getVersion(){
		return version;
	}
}